package com.xbongbong.util;

import com.xbongbong.paas.pojo.DetailTabPojo;
import com.xbongbong.paas.pojo.WorkFlowPojo;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.form.pojo.TopPermissionsPoJo;
import com.xbongbong.saas.enums.BusinessDetailTabEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/02/10 15:29
 */
public class NameUtil {

    public static void changeValue(Map<Integer, String> paasBusinessTypeToName, TopPermissionsPoJo topPermissionsPoJo) {
        StringBuilder newName = new StringBuilder();
        Integer pojoBusinessType = topPermissionsPoJo.getBusinessType();
        List<String> strings = Arrays.asList("new", "set");
        if (needToChangeName(paasBusinessTypeToName, pojoBusinessType) && strings.contains(topPermissionsPoJo.getBtnType())) {
            newName.append(topPermissionsPoJo.getValue().substring(0, 2)).append(paasBusinessTypeToName.get(pojoBusinessType));
            topPermissionsPoJo.setValue(newName.toString());
        }
    }

    /**
     * 改变名字
     * @param paasBusinessTypeToName
     * @param workFlowPojo
     */
    public static void changeValue(Map<Integer, String> paasBusinessTypeToName, WorkFlowPojo workFlowPojo) {
        StringBuilder newName = new StringBuilder();
        Integer pojoBusinessType = workFlowPojo.getBusinessType();
        Integer num  =2;
        Locale locale = LocaleContextHolder.getLocale();
        if (Objects.equals(locale.getLanguage(), I18nStringConstant.ENGLISH_LANGUAGE)) {
            num =3;
        }
        if (needToChangeName(paasBusinessTypeToName, pojoBusinessType)) {
            newName.append(workFlowPojo.getValue().substring(0,num)).append(paasBusinessTypeToName.get(pojoBusinessType));
            workFlowPojo.setValue(newName.toString());
        }
    }

    /**
     * 如果菜单名字改变,修改相应的表单名字
     * @param paasBusinessTypeToName
     * @param tabEnum
     * @param detailTabPojo
     */
    public static void changeTabEnumName(Map<Integer, String> paasBusinessTypeToName, BusinessDetailTabEnum tabEnum, DetailTabPojo detailTabPojo) {
        if (tabEnum.getFormBusinessType() != 0
                && Objects.nonNull(paasBusinessTypeToName.get(tabEnum.getFormBusinessType()))
                && !Objects.equals(paasBusinessTypeToName.get(tabEnum.getFormBusinessType()), XbbRefTypeEnum.getByCode(tabEnum.getFormBusinessType()).getName())) {
            detailTabPojo.setName(paasBusinessTypeToName.get(tabEnum.getFormBusinessType()));
        }
    }

    /**
     * 如果菜单名字改变,修改相应的表单名字
     * @param paasBusinessTypeToName
     * @param formBusinessType
     * @param detailTabPojo
     */
    public static void changeTabEnumName(Map<Integer, String> paasBusinessTypeToName, Integer formBusinessType, DetailTabPojo detailTabPojo) {
        if (formBusinessType != 0
                && Objects.nonNull(paasBusinessTypeToName.get(formBusinessType))
                && !Objects.equals(paasBusinessTypeToName.get(formBusinessType), XbbRefTypeEnum.getByCode(formBusinessType).getName())) {
            detailTabPojo.setName(paasBusinessTypeToName.get(formBusinessType));
        }
    }


    /**
     * 判断是否要换名字
     * @param paasBusinessTypeToName
     * @param pojoBusinessType
     * @return
     */
    public static boolean needToChangeName(Map<Integer, String> paasBusinessTypeToName, Integer pojoBusinessType) {
        return !Objects.equals(XbbRefTypeEnum.getByCode(pojoBusinessType).getName(), paasBusinessTypeToName.get(pojoBusinessType)) && StringUtil.isNotEmpty(paasBusinessTypeToName.get(pojoBusinessType));
    }
}
